package org.dromara.oce.controller.admin;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.enums.FormatsType;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.oce.domain.bo.IaaBackBo;
import org.dromara.oce.domain.dto.TradeOrderIaaBackRecordDTO;
import org.dromara.oce.domain.dto.TradeOrderIaaBackRecordExcelDTO;
import org.dromara.oce.service.OceIaaBackService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 获取iaa回传记录列表
 *
 * @author Lion Li
 * @date 2025-02-26
 * @module 管理平台接口
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/oce/iaaBack")
public class IaaPassBackController extends BaseController {

    private final OceIaaBackService oceIaaBackService;

    /**
     * 查询iaa回传记录列表
     */
    @SaCheckPermission("oce:iaaBack:list")
    @GetMapping("/list")
    public TableDataInfo<TradeOrderIaaBackRecordDTO> list(IaaBackBo bo, PageQuery pageQuery) {
        if(StringUtils.isBlank(bo.getStartDate())){
            String startDate = DateUtils.parseDateToStr(FormatsType.YYYY_MM_DD, new Date());
            bo.setStartDate(startDate + " 00:00:00");
        }
        if(StringUtils.isBlank(bo.getEndDate())){
            String startDate = DateUtils.parseDateToStr(FormatsType.YYYY_MM_DD, new Date());
            bo.setEndDate(startDate + " 23:59:59");
        }
        return oceIaaBackService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出iaa回传记录列表
     */
    @SaCheckPermission("oce:iaaBack:export")
    @Log(title = "iaa回传记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IaaBackBo bo, HttpServletResponse response) {
        if(StringUtils.isBlank(bo.getStartDate())){
            String startDate = DateUtils.parseDateToStr(FormatsType.YYYY_MM_DD, new Date());
            bo.setStartDate(startDate + " 00:00:00");
        }
        if(StringUtils.isBlank(bo.getEndDate())){
            String startDate = DateUtils.parseDateToStr(FormatsType.YYYY_MM_DD, new Date());
            bo.setEndDate(startDate + " 23:59:59");
        }
        List<TradeOrderIaaBackRecordExcelDTO> list = oceIaaBackService.queryList(bo);
        ExcelUtil.exportExcel(list, "iaa回传记录", TradeOrderIaaBackRecordExcelDTO.class, response);
    }

    /**
     * iaa手动上报
     */
    @SaCheckPermission("oce:iaaBack:reportIaaBack")
    @GetMapping("/reportIaaBack/{id}")
    public R<Boolean> reportIaaBack(@NotNull(message = "主键不能为空") @PathVariable Long id) {
        return R.ok(oceIaaBackService.reportIaaBack(id));
    }

}
